package com.cb.controller.admin.system;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.system.AppRole;
import com.cb.domain.service.system.AppRoleManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */

public class AppRoleFormController extends BaseFormController {
	@Autowired
	private AppRoleManager appRoleManager=null;
	
	  public AppRoleFormController() { 
          setCommandName("appRole"); 
          setCommandClass(AppRole.class); 
	  } 

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		AppRole appRole=(AppRole)command;
		String msg=null;
		//validate the logic business data here [TODO]
		if(appRole.getRoleId()==null){
			msg=getText("appRole.added",appRole.getRoleId());
		}else{
			msg=getText("appRole.updated",appRole.getRoleId());
		}
		
		appRoleManager.save(appRole);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		AppRole appRole=null;
		String roleId=request.getParameter("roleId");
		if(StringUtils.isNotEmpty(roleId)){
			appRole=appRoleManager.get(new Long(roleId));
		}else{
			appRole=new AppRole();
		}
		return appRole;
	}
	
}
